package com.example.taobaou.presenter.impl;

import com.example.taobaou.model.Api;
import com.example.taobaou.model.domain.TicketParams;
import com.example.taobaou.model.domain.TicketResult;
import com.example.taobaou.presenter.ITicketPresenter;
import com.example.taobaou.utils.LogUtils;
import com.example.taobaou.utils.RetrofitManager;
import com.example.taobaou.utils.UrlUtils;
import com.example.taobaou.view.ITicketPagerCallback;
import com.lcodecore.tkrefreshlayout.utils.LogUtil;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TicketPresenterImpl implements ITicketPresenter {
    private ITicketPagerCallback mViewCallback = null;
    private String mCover = null;
    private TicketResult ticketResponse;

    enum LoadState {
        LOADING, SUCCESS, ERROR, NONE
    }

    private LoadState currentState = LoadState.NONE;

    @Override
    public void getTicket(String title, String url, String cover) {
        this.mCover = cover;
        onTicletLoading();
//        LogUtils.d(this, "title------>" + title);
//        LogUtils.d(this, "url------>" + url);
//        LogUtils.d(this, "cover------>" + cover);
        String ticketUrl = UrlUtils.getTicket(url);
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        Api api = retrofit.create(Api.class);
        TicketParams ticketParams = new TicketParams(ticketUrl, title);
        Call<TicketResult> task = api.getTicket(ticketParams);
        task.enqueue(new Callback<TicketResult>() {

            @Override
            public void onResponse(Call<TicketResult> call, Response<TicketResult> response) {
                int code = response.code();
                //LogUtils.d(TicketPresenterImpl.this, "response code----------->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    //请求成功
                    ticketResponse = response.body();
                    //LogUtils.d(TicketPresenterImpl.this, "response" + ticketResponse);
                    //通知ui更新
                    onTicketLoadSuccess();
                } else {
                    //请求失败
                    onLoadedTicketError();
                }
            }

            @Override
            public void onFailure(Call<TicketResult> call, Throwable t) {
                //请求失败
                onLoadedTicketError();
            }
        });

    }

    @Override
    public void registerViewCallback(ITicketPagerCallback callback) {
        if (currentState != LoadState.NONE) {
            //说明状态已经被改变了
            //更新ui
            if (currentState == LoadState.SUCCESS) {
                //加载成功
                onTicketLoadSuccess();

            } else if (currentState == LoadState.ERROR) {
                //加载错误
                onLoadedTicketError();
            } else if (currentState == LoadState.LOADING) {
                //加载中
                onTicletLoading();
            }
        }
        this.mViewCallback = callback;
    }

    @Override
    public void unregisiterViewCallback(ITicketPagerCallback callback) {
        this.mViewCallback = null;
    }

    private void onLoadedTicketError() {
        if (mViewCallback != null) {
            mViewCallback.onNatworkError();
        } else {
            currentState = LoadState.ERROR;
        }
    }

    private void onTicketLoadSuccess() {
        if (mViewCallback != null) {
            mViewCallback.onTicketLoaded(mCover, ticketResponse);
        } else {
            currentState = LoadState.SUCCESS;
        }
    }

    private void onTicletLoading() {
        if (mViewCallback != null) {
            mViewCallback.onLoading();
        } else {
            currentState = LoadState.LOADING;
        }
    }
}
